package com.arsl.client.view;

import java.util.Date;
import java.util.List;

import com.arsl.client.GlobalInfo;
import com.arsl.client.data.Paper;
import com.arsl.client.data.SectionStats;
import com.arsl.client.util.Constants;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.datepicker.client.DateBox;
import com.google.gwt.view.client.ListDataProvider;

public class AnalyticsView extends Composite {

	private static AnalyticsViewUiBinder uiBinder = GWT
			.create(AnalyticsViewUiBinder.class);

	interface AnalyticsViewUiBinder extends UiBinder<Widget, AnalyticsView> {
	}

	@UiField
	ListBox lstPaper;

	@UiField
	DateBox dtStartDate;

	@UiField
	DateBox dtEndDate;

	@UiField
	HorizontalPanel pnlTable;

	@UiField
	Button btnGo;

	@UiHandler("btnGo")
	public void onGoClick(ClickEvent event) {
		int paperId = getPaperId();

		Date date = dtStartDate.getValue();
		long t = date.getTime();
		java.sql.Date sdate = new java.sql.Date(t);

		date = dtEndDate.getValue();
		t = date.getTime();
		java.sql.Date edate = new java.sql.Date(t);

		GlobalInfo.rpcService.getHitStats(paperId, sdate, edate,
				new AsyncCallback<SectionStats[]>() {

					@Override
					public void onSuccess(SectionStats[] result) {
						showHitStats(result);
					}

					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub

					}
				});
	}

	protected void showHitStats(SectionStats[] stats) {
		CellTable<SectionStats> table = new CellTable<SectionStats>();

		TextColumn<SectionStats> sectionNamesCol = new TextColumn<SectionStats>() {

			@Override
			public String getValue(SectionStats sectionStats) {
				// TODO Auto-generated method stub
				return sectionStats.sectionName == null ? ""
						: sectionStats.sectionName;
			}
		};

		TextColumn<SectionStats> hitsCol = new TextColumn<SectionStats>() {

			@Override
			public String getValue(SectionStats sectionStats) {
				// TODO Auto-generated method stub
				return sectionStats.hits + "";
			}
		};

		TextColumn<SectionStats> pageCntCol = new TextColumn<SectionStats>() {

			@Override
			public String getValue(SectionStats sectionStats) {
				// TODO Auto-generated method stub
				return sectionStats.arPageCnt + "";
			}
		};

		table.addColumn(sectionNamesCol, "Section");
		table.addColumn(hitsCol, "Total Hits");
		table.addColumn(pageCntCol, "Augmented Pages");

		// Create a data provider.
		ListDataProvider<SectionStats> dataProvider = new ListDataProvider<SectionStats>();

		// Connect the table to the data provider.
		dataProvider.addDataDisplay(table);

		// Add the data to the data provider, which automatically pushes it to
		// the
		// widget.
		int ttlHits = 0;
		int ttlArPages = 0;
		List<SectionStats> list = dataProvider.getList();
		for (SectionStats sectionStat : stats) {
			list.add(sectionStat);
			ttlHits += sectionStat.hits;
			ttlArPages += sectionStat.arPageCnt;
		}
		SectionStats ttlStat = new SectionStats();
		ttlStat.sectionName = "Total";
		ttlStat.hits = ttlHits;
		ttlStat.arPageCnt = ttlArPages;
		list.add(ttlStat);

		pnlTable.clear();
		pnlTable.add(table);
	}

	public void setPapers(Paper[] papers) {
		lstPaper.clear();
		for (int i = 0; i < papers.length; i++) {
			lstPaper.addItem(papers[i].getName(),
					String.valueOf(papers[i].getId()));
		}
	}

	public int getPaperId() {
		int selIndex = lstPaper.getSelectedIndex();
		int paperId = Integer.parseInt(lstPaper.getValue(selIndex));
		return paperId;
	}

	public void populatePapers() {
		GlobalInfo.rpcService.getPapers(GlobalInfo.publisherId,
				new AsyncCallback<Paper[]>() {

					@Override
					public void onSuccess(Paper[] result) {
						setPapers(result);
					}

					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub

					}
				});
	}

	public AnalyticsView() {
		initWidget(uiBinder.createAndBindUi(this));
		populatePapers();

		Date now = new Date();
		dtEndDate.setValue(now);
		dtEndDate.setFormat((new DateBox.DefaultFormat(DateTimeFormat
				.getFormat(PredefinedFormat.DATE_SHORT))));
		now.setTime(now.getTime() - Constants.DAY_IN_MILIS);
		dtStartDate.setValue(now);
		dtStartDate.setFormat((new DateBox.DefaultFormat(DateTimeFormat
				.getFormat(PredefinedFormat.DATE_SHORT))));

	}

}
